package com.android.inestpin.fragment.detail;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.inestpin.R;
import com.android.inestpin.fragment.BaseLazyFragment;
import com.android.inestpin.model.detail.BrandGoodsListData;
import com.android.inestpin.model.detail.BrandGoodsListDataCallback;
import com.android.inestpin.utils.ConstantsUrl;
import com.android.inestpin.utils.HttpUtils;
import com.android.inestpin.utils.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by Administrator on 2017/6/29.
 */

public class Fragment_BrandGoodsList extends BaseLazyFragment {
    @BindView(R.id.no_data)
    RelativeLayout noData;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;
    @BindView(R.id.tab)
    TabLayout tabLayout;
    @BindView(R.id.bgoodslist_iv)
    ImageView bgoodslist_iv;
    @BindView(R.id.pager)
    ViewPager viewPager;
    private static final String ARG_PARAM1 = "param1";
    private LayoutInflater inflater;
    private View view;
    private FragmentActivity mcontext;
    private boolean networkConnected;
    private GroupFragmentPagerAdapters pagerAdapter;
    private StaggeredGridLayoutManager mLayoutManager;
    private ArrayList<BrandGoodsListData.DataBean.Brand_list> brand_lists = new ArrayList<>();
    private int page = 1;
    private String top1;

    //private String top2;
    public static Fragment_BrandGoodsList newInstance(String top1) {
        Fragment_BrandGoodsList fragment = new Fragment_BrandGoodsList();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, top1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            top1 = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        view = inflater.inflate(R.layout.fragment_brandgoodslist, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initPrepare() {
        initUI();
    }

    @Override
    protected void onInvisible() {

    }

    @Override
    protected void initData() {
        GetBrandGoodsList(top1);
    }

    private void GetBrandGoodsList(final String top) {
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            String address = ConstantsUrl.domain + ConstantsUrl.BRANDGOODSLIST;
            HashMap<String, String> map = new HashMap<>();
            map.put("top", top + "");
            HttpUtils.get(address, map, new BrandGoodsListDataCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
                }

                @Override
                public void onResponse(BrandGoodsListData response, int id) {
                    if(response == null){
                        ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
                        return;
                    }
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
                    if (response.getCode() == 0) {
                        BrandGoodsListData.DataBean data = response.getData();
                        if(data!=null){
                            if (data.getRow() != null && data.getRow().getThumb() != null) {
                                ObjectUtils.photo2(mcontext, data.getRow().getThumb(), bgoodslist_iv);
                            }
                            List<BrandGoodsListData.DataBean.Brand_list> brand_list = data.getBrand_list();
                            if (ObjectUtils.listHasData(brand_list)) {
                                brand_lists.add(new BrandGoodsListData.DataBean.Brand_list(top1, "全部"));
                                brand_lists.addAll(brand_list);
                                //top2 = response.getData().getRow().getTop()+"";
//                                ObjectUtils.photo2(mcontext, data.getRow().getThumb(), bgoodslist_iv);
                                pagerAdapter = new GroupFragmentPagerAdapters(mcontext.getSupportFragmentManager());
                                viewPager.setAdapter(pagerAdapter);
                                tabLayout.setupWithViewPager(viewPager);
                            }
                        }
                    } else {
                        noData.setVisibility(View.VISIBLE);
                        noDataTv.setText(response.getMsg());
                        noDataTv.setVisibility(View.VISIBLE);
                    }
                }
            });
        } else {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
            ObjectUtils.toast(mcontext, "连接连接出现异常");
        }
    }

    private void initUI() {

    }

    private class GroupFragmentPagerAdapters extends FragmentPagerAdapter {

        public GroupFragmentPagerAdapters(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return Fragment_TabBrandGoodsList.newInstance(brand_lists.get(position).getId());
        }

        @Override
        public int getCount() {
            return brand_lists.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return brand_lists.get(position).getCatname();
        }
    }
}
